# !/usr/bin/env python
# -*- coding:utf-8 -*-
from modelDb.baseModel import baseModel
import pymysql

class touristsModel(baseModel):
    def __init__(self):
        super(touristsModel, self).__init__()
        self._table = "tourists"

    # ====================================================
    # 添加客源
    # ====================================================
    def add(self, data):
        cursor = self.conn.cursor()
        sql = """
            insert into tourists(
                imei,idfa,city, source_id, phone,etl_status,tourist_class
            ) values(
                '%(imei)s','%(idfa)s','%(city)s', %(source_id)s, '%(phone)s',%(etl_status)s,%(tourist_class)s
            )""" % data
        cursor.execute(sql)
        new_id = cursor.lastrowid


        self.conn.commit()
        cursor.close()
        return new_id



    #====================================================
    # 通过手机查找客源数据
    #====================================================
    def findByPhone(self, phone):
        cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        sql = "select * from %(table)s where phone = '%(phone)s'" % {"table":self._table, "phone":phone}
        cursor.execute(sql)
        result = cursor.fetchone()

        #self.conn.commit()
        cursor.close()
        return result

    #====================================================
    # 通过id查找客源数据
    #====================================================
    def findById(self, id):
        cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        sql = "select * from %(table)s where id = '%(id)s'" % {"table":self._table, "id":id}
        cursor.execute(sql)
        result = cursor.fetchone()

        #self.conn.commit()
        cursor.close()
        return result


    #====================================================
    # 获取etl清洗的数据
    #====================================================
    # def selectEtl(self):
    #     cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
    #
    #     sql = "select id, phone, etl_status, tourist_class  from tourists where etl_status = 1 limit 1000"
    #     cursor.execute(sql)
    #     result = cursor.fetchall()
    #
    #     cursor.close()
    #     return result

    #====================================================
    # 跟新etl清洗的数据
    #====================================================

    def updateEtl(self, id, data):
        cursor = self.conn.cursor()
        data['id'] = id
        sql = "update tourists set etl_status=2, tourist_class=%(tourist_class)s where id=%(id)s" % data
        effect_row = cursor.execute(sql)
        self.conn.commit()

        # 关闭游标
        cursor.close()
        return effect_row


    # ====================================================
    # 跟新etl清洗的数据
    # ====================================================

    def updateCheckToEtl(self, id, etl_status):
        cursor = self.conn.cursor()

        sql = "update tourists set etl_status=%d where id=%d " % (etl_status, id)
        effect_row = cursor.execute(sql)
        self.conn.commit()

        # 关闭游标
        cursor.close()
        return effect_row



    # ====================================================
    # 更新Etl
    # ====================================================

    def updateEtlValidaById(self, id, data):
        cursor = self.conn.cursor()
        data.setdefault('id', id)
        sql = "update tourists set status=%(status)d, etl_status=%(etl_status)d, tourist_class=%(tourist_class)d where id=%(id)d " % data
        effect_row = cursor.execute(sql)
        self.conn.commit()

        # 关闭游标
        cursor.close()
        return effect_row

